<?php


namespace app\hapi\controller;


use app\api\controller\CarAct;
use app\common\model\Moncar;
use app\common\model\Parkinfo;
use app\common\model\TicketNo;

class Caradmin extends ApiBase
{
    public function moncarlist($param)
    {
        $query = json_decode($param['query'], true);
        $page = ($query['page'] - 1) * $query['limit'];
        $map = [];
        $cacheinfo=$this->getcacheinfo($param['token']);
        if($cacheinfo['parkid']){
            $data['a.parkid']=$cacheinfo['parkid'];
        }
        if (isset($query['carno'])) {
            $map['a.carno'] = array('like', "%" . $query['carno'] . "%");
        }
        $MonCar = new Moncar();
        $list = $MonCar
            ->alias('a')
            ->join('wn_parkinfo b', 'a.parkid=b.id')
            ->where($map)->limit($page, $query['limit'])->order('id desc')
            ->limit($page, $query['limit'])
            ->field('a.*,b.parktitle as parktitle')
            ->order('a.id desc')->select();
        foreach($list as $k=>$v){
            $list[$k]['cardstatus']=$v['cardstatus']==0?"正常":"异常";
        }
        $count = $MonCar->alias('a')->where($map)->count();
        $re_data['return_code'] = "SUCCESS";
        $re_data['return_msg'] = "成功";
        $re_data['data']['items'] = $list;
        $re_data['data']['total'] = $count;
        return $re_data;

    }

    public function moncaradd($param)
    {
        $cacheinfo=$this->getcacheinfo($param['token']);
        $query = json_decode($param['query'], true);
        $lanelist = implode('_', $query['lanelist']);
        $s_data['parkid'] = $query['parkid'];
        $s_data['cartype'] = $query['cartype'];
        $s_data['carno'] = $query['carno'];
        $s_data['username'] = $query['username'];
        $s_data['phone'] = $query['phone'];
        $s_data['cardstatus'] = "0";
        $s_data['starttime'] = $query['starttime'];
        $s_data['endtime'] = $query['endtime'];
        $s_data['lanelist'] = $lanelist;
        $s_data['setno'] = $query['setno'];
        $s_data['create_time']=time();
        $s_data['create_user']=$cacheinfo['username'];
        $CarAct = new CarAct();
        $res = $CarAct->moncaract($s_data, "Add");
        if ($res['return_code'] == "SUCCESS") {
            $add = Moncar::Create($s_data);
            if ($add) {
                $re_data['return_code'] = "SUCCESS";
                $re_data['return_msg'] = "成功";
            } else {
                $re_data['return_code'] = "FAIL";
                $re_data['return_msg'] = "添加失败";
            }
        } else {
            $re_data = $res;
        }

        return $re_data;
    }

    public function moncaredit($param)
    {
        $query = json_decode($param['query'], true);
        $lanelist = implode('_', $query['lanelist']);
        $s_data['id'] = $query['id'];
        $s_data['parkid'] = $query['parkid'];
        $s_data['cartype'] = $query['cartype'];
        $s_data['carno'] = $query['carno'];
        $s_data['username'] = $query['username'];
        $s_data['phone'] = $query['phone'];
        $s_data['cardstatus'] = "0";
        $s_data['starttime'] = $query['starttime'];
        $s_data['endtime'] = $query['endtime'];
        $s_data['lanelist'] = $lanelist;
        $s_data['setno'] = $query['setno'];
        $CarAct = new CarAct();
        if(isset($query['uptype']) && $query['uptype']=="Delay"){
            $uptype="Delay";
        }else{
            $uptype="Edit";
        }
        $res = $CarAct->moncaract($s_data, $uptype);
        if ($res['return_code'] == "SUCCESS") {
            $add = Moncar::update($s_data);
            if ($add) {
                $re_data['return_code'] = "SUCCESS";
                $re_data['return_msg'] = "成功";
            } else {
                $re_data['return_code'] = "FAIL";
                $re_data['return_msg'] = "添加失败";
            }
        }else{
            $re_data['return_code'] = "FAIL";
            $re_data['return_msg'] = $res['return_msg'];
        }
        return $re_data;
    }

    public function moncardel($param)
    {
        $query = json_decode($param['query'], true);
        $CarAct = new CarAct();
        $s_data = Moncar::get($query['id']);
        $res = $CarAct->moncaract($s_data, "Del");
        if ($res['return_code'] == "SUCCESS") {
            $del = Moncar::destroy(['id' => $query['id']]);
            if ($del) {
                $re_data['return_code'] = "SUCCESS";
                $re_data['return_msg'] = "成功";
            } else {
                $re_data['return_code'] = "FAIL";
                $re_data['return_msg'] = "删除失败";
            }
        } else {
            $re_data['return_code'] = "FAIL";
            $re_data['return_msg'] = $res['return_msg'];
        }
        return $re_data;
    }
    //停车券管理
    public function carticketlist($param){

        $query = json_decode($param['query'], true);
        $page = ($query['page'] - 1) * $query['limit'];
        $map = [];
        if (isset($query['carno'])) {
            $map['a.carno'] = array('like', "%" . $query['carno'] . "%");
        }
        $cacheinfo=$this->getcacheinfo($param['token']);
        if($cacheinfo['parkid']){
            $data['a.parkid']=$cacheinfo['parkid'];
        }
        $TicketNo = new TicketNo();
        $list = $TicketNo
            ->alias('a')
            ->join('wn_parkinfo b', 'a.parkid=b.id')
            ->where($map)->limit($page, $query['limit'])->order('id desc')
            ->limit($page, $query['limit'])
            ->field('a.*,b.parktitle as parktitle')
            ->order('a.id desc')->select();
        foreach($list as $k=>$v){
            $list[$k]['status']=$v['status']=="Y"?"已使用":"未使用";
            $list[$k]['dist_type']= getdist_type($v['dist_type']);
        }
        $count = $TicketNo->alias('a')->where($map)->count();
        $re_data['return_code'] = "SUCCESS";
        $re_data['return_msg'] = "成功";
        $re_data['data']['items'] = $list;
        $re_data['data']['total'] = $count;
        return $re_data;
    }
    public function ticketnoadd($param)
    {
        $cacheinfo=$this->getcacheinfo($param['token']);

        $query = json_decode($param['query'], true);
        $s_data['parkid'] = $query['parkid'];
        $s_data['carno'] = $query['carno'];
        $s_data['username'] = $query['username'];
        $s_data['phone'] = $query['phone'];
        $s_data['starttime'] = $query['starttime'];
        $s_data['endtime'] = $query['endtime'];
        $s_data['dist_type'] = $query['dist_type'];
        $s_data['dist_fee'] = $query['dist_fee'];
        $s_data['create_time'] = time();
        $s_data['create_user'] = $cacheinfo['username'];
        $CarAct = new CarAct();
        $res = $CarAct->carticketnoact($s_data, "Add");
        if ($res['return_code'] == "SUCCESS") {
            $s_data['ticket_no']=$res['ticket_no'];
            $add = TicketNo::Create($s_data);
            if ($add) {
                $re_data['return_code'] = "SUCCESS";
                $re_data['return_msg'] = "成功";
            } else {
                $re_data['return_code'] = "FAIL";
                $re_data['return_msg'] = "添加失败";
            }
        } else {
            $re_data = $res;
        }

        return $re_data;
    }

    public function ticketnoedit($param)
    {
        $query = json_decode($param['query'], true);
        $s_data['id'] = $query['id'];
        $s_data['parkid'] = $query['parkid'];
        $s_data['carno'] = $query['carno'];
        $s_data['username'] = $query['username'];
        $s_data['phone'] = $query['phone'];
        $s_data['starttime'] = $query['starttime'];
        $s_data['endtime'] = $query['endtime'];
        $s_data['dist_type'] = $query['dist_type'];
        $s_data['dist_fee'] = $query['dist_fee'];
        $CarAct = new CarAct();
        $res = $CarAct->carticketnoact($s_data, "Edit");
        if ($res['return_code'] == "SUCCESS") {
            $add = TicketNo::update($s_data);
            if ($add) {
                $re_data['return_code'] = "SUCCESS";
                $re_data['return_msg'] = "成功";
            } else {
                $re_data['return_code'] = "FAIL";
                $re_data['return_msg'] = "添加失败";
            }
        }else{
            $re_data['return_code'] = "FAIL";
            $re_data['return_msg'] = $res['return_msg'];
        }
        return $re_data;
    }

    public function ticketnodel($param)
    {
        $query = json_decode($param['query'], true);
        $CarAct = new CarAct();
        $s_data = TicketNo::get($query['id']);
        $res = $CarAct->carticketnoact($s_data, "Del");
        if ($res['return_code'] == "SUCCESS") {
            $del = Moncar::destroy(['id' => $query['id']]);
            if ($del) {
                $re_data['return_code'] = "SUCCESS";
                $re_data['return_msg'] = "成功";
            } else {
                $re_data['return_code'] = "FAIL";
                $re_data['return_msg'] = "删除失败";
            }
        } else {
            $re_data['return_code'] = "FAIL";
            $re_data['return_msg'] = $res['return_msg'];
        }
        return $re_data;
    }
}